package com.lhhu.demo.service.impl;

import com.alibaba.fastjson.JSON;
import com.lhhu.demo.bean.BookMessage;
import com.lhhu.demo.mapper.BooksMapper;
import com.lhhu.demo.service.BooksService;
import com.lhhu.demo.service.ClassifyService;
import com.lhhu.demo.utils.RedisClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Auther 虎
 * @Data 2021/3/6 11:31
 * @Version 1.0
 */
@Service
public class BooksServiceImpl implements BooksService{
    @Autowired
    private BooksMapper booksMapper;
    @Autowired
    private ClassifyService classifyService;

    @Autowired
    private RedisClient redisClient;
    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @Override
    public List<BookMessage> getAllBookMessage() {
        return booksMapper.getAllBookMessage();
    }

    @Override
    public int addOneBookMessage(String codes,BookMessage bookMessage) {
        bookMessage.setBookDiscount(bookMessage.getBookPprice()/bookMessage.getBookOprice()*10);
        Date date = new Date(System.currentTimeMillis());
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyMMddHHmmss");
        int random = (int)(Math.random()*10);
        String bookId ='1'+ dateFormat.format(date) + random;
        bookMessage.setBookId(bookId);

        String bookCategory = classifyService.getOneBookClassifyById(bookMessage.getBookCategoryId());
        bookMessage.setBookCategory(bookCategory);

        //redis
        redisClient.set(bookId, JSON.toJSONString(bookMessage));
        return booksMapper.addOneBookMessage(bookMessage);
    }

    @Override
    public int deleteOneBookMessage(String codes,String bookId) {
        int flag =  booksMapper.deleteOneBookMessage(bookId);
        Boolean item = redisClient.hasKey(bookId);
        if(flag==1&&item) redisClient.del(bookId);
        return flag;
    }

    @Override
    public int updateOneBookMessage(String codes,BookMessage bookMessage) {
        bookMessage.setBookDiscount(bookMessage.getBookPprice()/bookMessage.getBookOprice()*10);
        int flag = booksMapper.updateOneBookMessage(bookMessage);
        if(flag==1){
            //redis
            String bookId = bookMessage.getBookId();
            Boolean item = redisClient.hasKey(bookId);
            if(item) redisClient.del(bookId);
            redisClient.set(bookId, JSON.toJSONString(bookMessage));
        }
        return flag;
    }

    @Override
    public List<BookMessage> getOneBookMessageById(String bookId) {
        List<BookMessage> list=new ArrayList<BookMessage>();
        BookMessage bookMessage;
        Boolean item = redisClient.hasKey(bookId);
        if(item){
            String s = redisClient.get(bookId).toString();
            bookMessage = JSON.parseObject(s, BookMessage.class);
        }else{
            bookMessage = booksMapper.getOneBookMessageById(bookId).get(0);
            System.out.println(bookMessage.toString());
            redisClient.set(bookId, JSON.toJSONString(bookMessage));
        }
        list.add(bookMessage);
        return list;
    }
    @Override
    public List<BookMessage> getAllBookMessageByISBN(String bookISBN) {
        List<BookMessage> list=new ArrayList<BookMessage>();
        BookMessage bookMessage;
        Boolean item = redisClient.hasKey(bookISBN);
        if(item){
            String s = redisClient.get(bookISBN).toString();
            bookMessage = JSON.parseObject(s, BookMessage.class);
        }else{
            bookMessage = booksMapper.getAllBookMessageByISBN(bookISBN).get(0);
            redisClient.set(bookISBN, JSON.toJSONString(bookMessage));
        }
        list.add(bookMessage);
        return list;
    }

    @Override
    public List<BookMessage> getAllBookMessageByName(String bookName) {
        return booksMapper.getAllBookMessageByName(bookName);
    }

    @Override
    public List<BookMessage> getAllBookMessageByCategoryId(String bookCategoryId) {
        return booksMapper.getAllBookMessageByCategoryId(bookCategoryId);
    }

    @Override
    public List<BookMessage> getAllBookMessageByCategory(String bookCategory) {
        return booksMapper.getAllBookMessageByCategory(bookCategory);
    }

    @Override
    public List<BookMessage> getAllBookMessageByTag(String bookTag) {
        return booksMapper.getAllBookMessageByTag(bookTag);
    }
}
